//
//  CustomLayoutExampleViewController.swift
//  ChatExample
//
//  Created by Vignesh J on 30/04/21.
//  Copyright © 2021 MessageKit. All rights reserved.
//

import Kingfisher
import MapKit
//import MessageKit
import UIKit

class CustomChatLayoutViewController: ChatViewController {
  // MARK: Internal

    
  override func configureMessageCollectionView() {
    super.configureMessageCollectionView()
    messagesCollectionView.register(CustomTextMessageContentCell.self)
      
    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messagesLayoutDelegate = self
    messagesCollectionView.messagesDisplayDelegate = self
      messagesCollectionView.keyboardDismissMode = .onDrag
      
//      // 声明一个 UITapGestureRecognizer 识别手势
//      let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
//      
//      // 将手势添加到视图上
//      messagesCollectionView.addGestureRecognizer(tapGesture)
  }

    func isTimeLabelVisible(at indexPath: IndexPath) -> Bool {
      false
    }
    
    @objc func hideKeyboard() {
        // 在点击空白处时隐藏键盘
        messagesCollectionView.endEditing(true)
    }
    
    override func textCellSizeCalculator(
      for _: MessageType,
      at _: IndexPath,
      in _: MessagesCollectionView)
      -> CellSizeCalculator?
    {
      textMessageSizeCalculator
    }

    // MARK: - MessagesDataSource

    override func textCell(
      for message: MessageType,
      at indexPath: IndexPath,
      in messagesCollectionView: MessagesCollectionView)
      -> UICollectionViewCell?
    {
      let cell = messagesCollectionView.dequeueReusableCell(
        CustomTextMessageContentCell.self,
        for: indexPath)
      cell.configure(
        with: message,
        at: indexPath,
        in: messagesCollectionView,
        dataSource: self,
        and: textMessageSizeCalculator)

      return cell
    }

    // MARK: Private

    private lazy var textMessageSizeCalculator = CustomTextLayoutSizeCalculator(
      layout: self.messagesCollectionView
        .messagesCollectionViewFlowLayout)
  }
